﻿Imports System.Data.SqlClient

Public Class NguoiDangBUS

    Public Shared Function GetAll() As List(Of NguoiDangDTO)
        Dim listNguoiDang As New List(Of NguoiDangDTO)

        listNguoiDang = NguoiDangDAO.GetAll()

        Return listNguoiDang
    End Function
    Public Shared Function LayNguoiDangTamXoa() As List(Of NguoiDangDTO)
        Dim listNguoiDang As New List(Of NguoiDangDTO)

        listNguoiDang = NguoiDangDAO.LayNguoiDangTamXoa()

        Return listNguoiDang
    End Function
    Public Shared Function ExportDS_ChuNha() As DataTable
        Dim listChuNha As New DataTable()

        listChuNha = NguoiDangDAO.ExportDS_ChuNha()

        Return listChuNha
    End Function


    Public Shared Function LayDSHoTen() As DataTable
        Dim dt As DataTable = New DataTable()
        dt = NguoiDangDAO.LayDSHoTen()
        Return dt
    End Function

    Public Shared Sub Insert(ByVal nguoidang As NguoiDangDTO)
        Dim parameters(5) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNguoiDang", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strMaNguoiDang
        parameters(0) = para

        para = New SqlParameter("HoTen", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strHoTen
        parameters(1) = para

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strDiaChi
        parameters(2) = para

        para = New SqlParameter("DienThoai", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strDienThoai
        parameters(3) = para

        para = New SqlParameter("Email", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strEmail
        parameters(4) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.isTamXoa
        parameters(5) = para

        NguoiDangDAO.Insert(parameters)
    End Sub

    Public Shared Sub Delete(ByVal paraID As SqlParameter)
        NguoiDangDAO.Delete(paraID)
    End Sub

    Public Shared Sub Update_TamXoa(ByVal mand As String, ByVal istamxoa As Boolean)
        Dim parameters(1) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNguoiDang", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = mand
        parameters(0) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = istamxoa
        parameters(1) = para

        NguoiDangDAO.Update_TamXoa(parameters)
    End Sub

    Public Shared Sub Update(ByVal nguoidang As NguoiDangDTO)
        Dim parameters(5) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNguoiDang", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strMaNguoiDang
        parameters(0) = para

        para = New SqlParameter("HoTen", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strHoTen
        parameters(1) = para

        para = New SqlParameter("DiaChi", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strDiaChi
        parameters(2) = para

        para = New SqlParameter("DienThoai", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strDienThoai
        parameters(3) = para

        para = New SqlParameter("Email", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.strEmail
        parameters(4) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nguoidang.isTamXoa
        parameters(5) = para

        NguoiDangDAO.Update(parameters)
    End Sub

    Public Shared Function LayMa_ND_Cuoi() As String
        Dim macuoi As String
        macuoi = NguoiDangDAO.LayMa_ND_Cuoi()
        Return macuoi
    End Function

    Public Shared Function TinhMaNguoiDang(ByVal macuoi As String) As String
        Dim manguoidang As String = ""
        Dim num As Integer = macuoi.Substring(2) + 1
        If num < 10 Then
            manguoidang = "CN000" & num
        ElseIf num >= 10 And num < 100 Then
            manguoidang = "CN00" & num
        ElseIf num >= 100 And num < 1000 Then
            manguoidang = "CN0" & num
        ElseIf num >= 1000 Then
            manguoidang = "CN" & num
        End If

        Return manguoidang
    End Function

    Public Shared Function KiemTraDayDu(ByVal hoten As String, ByVal diachi As String, ByVal email As String, ByVal dienthoai As String)
        Dim kq As Boolean
        If Not hoten = "" Then
            If Not diachi = "" Then
                If Not email = "" Then
                    If Not dienthoai = "" Then
                        kq = True
                    Else
                        kq = False
                    End If
                Else
                    kq = False
                End If
            Else
                kq = False
            End If
        Else
            kq = False
        End If
        Return kq
    End Function
    Public Shared Function TimKiemTheoMa(ByVal mand As String) As List(Of NguoiDangDTO)
        Dim paraMa As New SqlParameter("manguoidang", SqlDbType.VarChar)
        paraMa.Direction = ParameterDirection.Input
        paraMa.Value = mand
        
        Dim listTimKiem As New List(Of NguoiDangDTO)
        listTimKiem = NguoiDangDAO.TimKiemTheoMa(paraMa)

        Return listTimKiem
    End Function

    Public Shared Function TimKiemTheTen(ByVal ten As String) As List(Of NguoiDangDTO)
        Dim paraTen As New SqlParameter("hoten", SqlDbType.NVarChar)
        paraTen.Direction = ParameterDirection.Input
        paraTen.Value = ten

        Dim listTimKiem As New List(Of NguoiDangDTO)
        listTimKiem = NguoiDangDAO.TimKiemTheoTen(paraTen)

        Return listTimKiem
    End Function
End Class
